package com.entor.hqj.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.entor.hqj.login.LoginActivity;
import com.entor.hqj.login.R;
import com.entor.hqj.model.User;
import com.entor.hqj.util.HttpPostUtils;
import com.entor.hqj.util.TitleBuilder;

public class EditPwdActivity extends Activity {

    private TextView old_pwd;
    private TextView new_pwd;
    private TextView cofr_pwd;
    private Long id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_pwd);

        old_pwd = (TextView)findViewById(R.id.et_oldPwd_edit) ;
        new_pwd = (TextView)findViewById(R.id.et_newPwd_edit);
        cofr_pwd = (TextView)findViewById(R.id.et_confirmPwd_edit);
        initTitle();
    }

    public void initTitle(){
        new TitleBuilder(this).setLeftText("取消").setRightText("确定")
        .setLeftImgOrTextListener(new leftImgOrTextListener())
        .setRightImgOrTextListener(new rightImgOrTextListener());
    }


    class leftImgOrTextListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            finish();
        }
    }

    /**
     * 监听修改密码事件
     */
    class rightImgOrTextListener implements View.OnClickListener{
        //TODO 右侧确定修改密码
        @Override
        public void onClick(View view) {
            update();
        }
    }

    public void update(){
        String old_ = old_pwd.getText().toString().trim();
        String new_ = new_pwd.getText().toString().trim();
        String com_ = cofr_pwd.getText().toString().trim();

        if(!new_.equals(com_)){
            Toast.makeText(EditPwdActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        User u = LoginActivity.userInfo;
        id = u.getId();

        final String data = "id="+id+"&oldPwd="+old_+"&pwd="+new_;
        new Thread() {
            @Override
            public void run() {
                String response =
                        HttpPostUtils.doPostRequest("user/editUserPwdByMobile", data);
                Message msg = Message.obtain();
                msg.obj = response;
                handler.sendMessage(msg);
            }
        }.start();


    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message message){
            String result = (String)message.obj;
            JSONObject resultJson =
                    JSON.parseObject(result);
            if(resultJson!=null) {
                String msg = resultJson.getString("msg");
                boolean sus = resultJson.getBoolean("success");
                User user = resultJson.getObject("obj",User.class);
                Toast.makeText(EditPwdActivity.this, msg, Toast.LENGTH_SHORT).show();
                if(sus&&user!=null){
                    LoginActivity.userInfo = user;
                    Intent intent = new Intent(EditPwdActivity.this,LoginActivity.class);
                    startActivity(intent);
                }

            }
        }
    };
}
